import 'package:OASys/route/myRoute.dart';
import 'package:toast/toast.dart';
import 'package:flutter/material.dart';
import '../../components/MenuDrawer.dart';
import '../../components/Layout.dart';
import 'personalPage.dart';
import 'callPage.dart';
import 'messagePage.dart';

class Tabs extends StatefulWidget {
  @override
  _TabsState createState() => _TabsState();
}

class _TabsState extends State<Tabs> {
  int _currentIndex = 0;
  Widget _currentPage = Layout();
  DateTime _lastQuitTime;

  void _onTap(int index) {
    switch (index) {
      case 0:
        _currentPage = Layout();
        break;
      case 1:
        _currentPage = MessagePage();
        break;
      case 2:
        _currentPage = CallPage();
        break;
      case 3:
        _currentPage = PersonalPage();
        break;
    }
    setState(() {
      _currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("办公系统"),
          centerTitle: true,
          automaticallyImplyLeading: false,
        ),
        endDrawer: Drawer(
          child: MenuDrawer(),
        ),
        body: WillPopScope(
          child: _currentPage,
          onWillPop: () async {
            if (_lastQuitTime == null ||
                DateTime.now().difference(_lastQuitTime).inSeconds > 1) {
              print('再按一次 Back 按钮退出');
              Toast.show('再次返回退出app', context);
              _lastQuitTime = DateTime.now();
              return false;
            } else {
              print('退出');
              Navigator.of(context).pop();
              return true;
            }
          },
        ),
        bottomNavigationBar: BottomNavigationBar(
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              label: '主页',
              icon: Icon(Icons.home_outlined),
            ),
            BottomNavigationBarItem(
              label: '消息通知',
              icon: Icon(Icons.mail_outline_outlined),
            ),
            BottomNavigationBarItem(
              label: '通讯录',
              icon: Icon(Icons.call_outlined),
            ),
            BottomNavigationBarItem(
              label: '个人中心',
              icon: Icon(Icons.account_circle_outlined),
            ),
          ],
          onTap: this._onTap,
          currentIndex: _currentIndex,
          type: BottomNavigationBarType.shifting, //配置底部可以有多个按钮
          selectedItemColor: Theme.of(context).primaryColor,
          unselectedItemColor: Color.fromRGBO(127, 127, 127, 1),
        ),
      ),
      onGenerateRoute: onGenerateRoute,
      onUnknownRoute: (settings) {
        print('无法识别');
        return MaterialPageRoute(builder: (context) => Tabs());
      },
      debugShowCheckedModeBanner: false,
    );
  }
}
